<?php
# 导入数据库接口文件
require_once 'dr-app-config.php';
require_once 'dr-app-functions.php';

require_once 'dr-db-reputations.php';

# 获得用户的请求
$action = isset($_POST['action']) ? $_POST['action'] : '';

switch ($action) {
	case 'question_up_vote':
		echo dr_question_up_vote();
		break;

	case 'question_down_vote':
		echo dr_question_down_vote();
		break;

	case 'answer_up_vote':
		echo dr_answer_up_vote();
		break;

	case 'answer_down_vote':
		echo dr_answer_down_vote();
		break;

	case 'comment_up_vote':
		echo dr_comment_up_vote();
		break;

	case 'comment_down_vote':
		echo dr_comment_down_vote();
		break;

	default:
		echo '';
}

function dr_question_up_vote()
{
	global $db;

	$id = $_POST['id'];
	$user_id = $_POST['user_id'];
	$owner_id = $_POST['owner_id'];
	$type = 'VQU';
	$object_id = $id;

	if (!dr_is_vote_valid('Q', $object_id, $user_id))
		return '{"ret":10}';

	//顶的投票数加一
	$result = dr_db_query_sub($db, 'update ^questions set up_votes_num=up_votes_num+1 where question_id=#', $id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票行为放入历史记录
	$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id) values(#,$,date_add(NOW(),interval 8 hour),#)', $user_id, $type, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票放入votes表中
	$result = dr_db_query_sub($db, 'insert into ^user_votes(user_id,vote_type,object_id,updown,vote_date) values(#,\'Q\',$,\'1\',date_add(NOW(),interval 8 hour))', $user_id, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	dr_db_update_user_reputation($user_id, 'upvote_send');
	dr_db_update_user_reputation($owner_id, 'upvote_get');

	return '{"ret":1}';
}

function dr_question_down_vote()
{
	global $db;

	$id = $_POST['id'];
	$user_id = $_POST['user_id'];
	$owner_id = $_POST['owner_id'];
	$type = 'VQD';
	$object_id = $id;

	if (!dr_is_vote_valid('Q', $object_id, $user_id))
		return '{"ret":10}';


	//踩的投票数加一
	$result = dr_db_query_sub($db, 'update ^questions set down_votes_num=down_votes_num+1 where question_id=#', $id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票行为放入历史记录
	$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id) values(#,$,date_add(NOW(),interval 8 hour),#)', $user_id, $type, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票放入votes表中
	$result = dr_db_query_sub($db, 'insert into ^user_votes(user_id,vote_type,object_id,updown,vote_date) values(#,\'Q\',$,\'-1\',date_add(NOW(),interval 8 hour))', $user_id, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	dr_db_update_user_reputation($user_id, 'upvote_send');
	dr_db_update_user_reputation($owner_id, 'upvote_get');

	return '{"ret":1}';
}

function dr_answer_up_vote()
{
	global $db;

	$id = $_POST['id'];
	$user_id = $_POST['user_id'];
	$owner_id = $_POST['owner_id'];
	$type = 'VAU';
	$object_id = $id;

	if (!dr_is_vote_valid('A', $object_id, $user_id))
		return '{"ret":10}';

	//顶的投票数加一
	$result = dr_db_query_sub($db, 'update ^replies set up_votes_num=up_votes_num+1 where reply_id=#', $id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票行为放入历史记录
	$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id) values(#,$,date_add(NOW(),interval 8 hour),#)', $user_id, $type, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票放入votes表中
	$result = dr_db_query_sub($db, 'insert into ^user_votes(user_id,vote_type,object_id,updown,vote_date) values(#,\'A\',$,\'1\',date_add(NOW(),interval 8 hour))', $user_id, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	dr_db_update_user_reputation($user_id, 'upvote_send');
	dr_db_update_user_reputation($owner_id, 'upvote_get');


	return '{"ret":1}';
}

function dr_answer_down_vote()
{
	global $db;

	$id = $_POST['id'];
	$user_id = $_POST['user_id'];
	$owner_id = $_POST['owner_id'];
	$type = 'VAD';
	$object_id = $id;

	if (!dr_is_vote_valid('A', $object_id, $user_id))
		return '{"ret":10}';

	//踩的投票数加一
	$result = dr_db_query_sub($db, 'update ^replies set down_votes_num=down_votes_num+1 where reply_id=#', $id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票行为放入历史记录
	$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id) values(#,$,date_add(NOW(),interval 8 hour),#)', $user_id, $type, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票放入votes表中
	$result = dr_db_query_sub($db, 'insert into ^user_votes(user_id,vote_type,object_id,updown,vote_date) values(#,\'A\',$,\'-1\',date_add(NOW(),interval 8 hour))', $user_id, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	dr_db_update_user_reputation($user_id, 'upvote_send');
	dr_db_update_user_reputation($owner_id, 'upvote_get');


	return '{"ret":1}';
}

function dr_comment_up_vote()
{
	global $db;

	$id = $_POST['id'];
	$user_id = $_POST['user_id'];
	$owner_id = $_POST['owner_id'];
	$type = 'VCU';
	$object_id = $id;

	if (!dr_is_vote_valid('C', $object_id, $user_id))
		return '{"ret":10}';

	//顶的投票数加一
	$result = dr_db_query_sub($db, 'update ^replies set up_votes_num=up_votes_num+1 where reply_id=#', $id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票行为放入历史记录
	$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id) values(#,$,date_add(NOW(),interval 8 hour),#)', $user_id, $type, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票放入votes表中
	$result = dr_db_query_sub($db, 'insert into ^user_votes(user_id,vote_type,object_id,updown,vote_date) values(#,\'C\',$,\'1\',date_add(NOW(),interval 8 hour))', $user_id, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	dr_db_update_user_reputation($user_id, 'upvote_send');
	dr_db_update_user_reputation($owner_id, 'upvote_get');


	return '{"ret":1}';
}

function dr_comment_down_vote()
{
	global $db;

	$id = $_POST['id'];
	$user_id = $_POST['user_id'];
	$owner_id = $_POST['owner_id'];
	$type = 'VCD';
	$object_id = $id;

	if (!dr_is_vote_valid('C', $object_id, $user_id))
		return '{"ret":10}';

	//踩的投票数加一
	$result = dr_db_query_sub($db, 'update ^replies set down_votes_num=down_votes_num+1 where reply_id=#', $id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票行为放入历史记录
	$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id) values(#,$,date_add(NOW(),interval 8 hour),#)', $user_id, $type, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	//将投票放入votes表中
	$result = dr_db_query_sub($db, 'insert into ^user_votes(user_id,vote_type,object_id,updown,vote_date) values(#,\'C\',$,\'-1\',date_add(NOW(),interval 8 hour))', $user_id, $object_id);
	if (!$result) {
		return '{"ret":0}';
	}

	dr_db_update_user_reputation($user_id, 'upvote_send');
	dr_db_update_user_reputation($owner_id, 'upvote_get');


	return '{"ret":1}';
}

function dr_is_vote_valid($type, $object_id, $user_id)
{
	global $db;
	$last_vote_date = dr_db_read_one_value(dr_db_query_sub($db, 'select max(vote_date) from ^user_votes where user_id=# and vote_type=$ and object_id=#', $user_id, $type, $object_id));

	if ($last_vote_date == false)
		return true;

	date_default_timezone_set("PRC");
	$now = date("Y-m-d H:i:s");

	if (strtotime($now) - strtotime($last_vote_date) < 3600 * 24)
		return false;
	else
		return true;

}